package com.finance.services.dao;

import com.finance.model.UserAccount;
import java.util.List;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Visual556
 */
@Repository
public class UserAccountDAO extends AbstractDAO<UserAccount> implements IUserAccountDAO {

    @Override
    public void create(UserAccount userAccount) throws Exception {
        long id = generateId();
        userAccount.setId(id);
        getSession().save(userAccount);
    }

    @Override
    public UserAccount read(String username) throws Exception {
        Query query = getSession().getNamedQuery("readUserAccountByUsername").setString("username", username);
        Object result = query.uniqueResult();

        if (util.check(result)) {
            return (UserAccount) result;
        }
        throw new Exception("User name not found.");
    }

    @Override
    public List<UserAccount> read() throws Exception {
        Query query = getSession().getNamedQuery("readAllUserAccounts");
        List<UserAccount> result = query.list();

        if (util.check(result)) {
            return result;
        }
        throw new Exception("Fail to read users from database.");
    }

    @Override
    public void update(UserAccount userAccount) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void delete(long id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
